package org.springmvc.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
import java.time.Instant;

/**
 * 使用了第三通用mapper
 * https://github.com/abel533/Mapper/wiki/2.3-generatedvalue
 *
 * @author tangjialin on 2018-08-13.
 */
public abstract class BaseEntity<E extends BaseEntity> implements Serializable {
    private static final long serialVersionUID = -1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @JsonIgnore
    @tk.mybatis.mapper.annotation.Version
    private Integer version;
    /** 创建时间 */
    private Instant createTime;
    /** 修改时间 */
    private Instant lastModifiedTime;
    /** 记录是否逻辑删除 。true 已删除，false 未删除 */
    private Boolean deleted;

    public Long getId() {
        return id;
    }

    public E setId(Long id) {
        this.id = id;
        return (E) this;
    }

    public Integer getVersion() {
        return version;
    }

    public E setVersion(Integer version) {
        this.version = version;
        return (E) this;
    }

    public Instant getCreateTime() {
        return createTime;
    }

    public E setCreateTime(Instant createTime) {
        this.createTime = createTime;
        return (E) this;
    }

    public Instant getLastModifiedTime() {
        return lastModifiedTime;
    }

    public E setLastModifiedTime(Instant lastModifiedTime) {
        this.lastModifiedTime = lastModifiedTime;
        return (E) this;
    }

    public Boolean getDeleted() {
        return deleted;
    }

    public E setDeleted(Boolean deleted) {
        this.deleted = deleted;
        return (E) this;
    }

    @Override
    public boolean equals(Object obj) {
        if (null != obj && obj.getClass() == this.getClass()) {
            Long eId = ((BaseEntity) obj).getId();
            return null != eId && null != id && id.longValue() == eId.longValue();
        }
        return false;
    }

    @Override
    public int hashCode() {
        return new StringBuilder().append(id).append(getClass()).hashCode();
    }

    @Override
    public String toString() {
        return this.getClass().getName() + ":" + id;
    }
}
